package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.convert.SubjectInfoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:刷题controller
 * @return:
 * @Author:  sky
 * @date:  2024/11/6 19:23
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    //@Resource
    @Autowired
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * @Description: 新增题目
     * @return:
     * @Author:  sky
     * @date:  2024/11/8 11:23
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){

        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(subjectInfoDTO));
            }

            // guava提供的断言检测功能，不匹配（false）则会直接抛出异常
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"难度不能为空");
            // 字符串需要同时检测， null 和 空串 两种情况，断言匹配false抛出异常
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),"题目名称不能为空");


            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertDTOListToBOList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOList);
            // alt + enter 快速生成返回变量
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * @Description: 分页查询题目列表
     * @return:
     * @Author:  sky
     * @date:  2024/11/8 17:06
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){

        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(subjectInfoDTO));
            }

            // guava提供的断言检测功能，不匹配（false）则会直接抛出异常
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"题目分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"题目标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            // alt + enter 快速生成返回变量
            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            PageResult<SubjectInfoDTO> subjectInfoDTOPageResult = SubjectInfoDTOConverter.INSTANCE.convertBoListToDTOList(boPageResult);
            return Result.ok(subjectInfoDTOPageResult);
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage(),e);
            return Result.fail("分页查询题目失败");
        }
    }

    /**
     * @Description: 查询单个题目的详情
     * @return:
     * @Author:  sky
     * @date:  2024/11/8 18:12
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){

        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(subjectInfoDTO));
            }

            // guava提供的断言检测功能，不匹配（false）则会直接抛出异常
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            SubjectInfoBO infoBO = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO infoDTO = SubjectInfoDTOConverter.INSTANCE.convertBoToDTO(infoBO);
            return Result.ok(infoDTO);
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }
}
